var sources = require('./../lib/source');
var getitem = require('./../lib/sourceitem')(sources);
module.exports = function (UC, source, config, ArFor, cache, package) {
    var UserControl = UC;
    var router = function (req, res, next) {

        if (req.UserAgent.isMobile) {
            res.redirect('./../../../../mobile/');
        } else {

            var login = false;

            if (UserControl.getoff(req.sessionID, 1)) {
                login = true;
            }

            new source(getitem('W_26'), {}, function (website) {
                new source(getitem('W_23'), {
                    id: 1
                }, function () {


                    var manifest_id = null;//cache.GetTemplateID(package.id+'/home');



                    var File = cache.GetTemplateID(package.id + "/" + website.id + "/home") + '.html';

                    cache.exists(File, function (exists) {
                        //exists=false;
                        if (!exists) {
                            var category = [{
                                title: '首页',
                                id: 'Home',
                                active: true
                            }];
                            new source(getitem("W_17"), {
                                'website': '1'
                            }, function (db) {
                                if (db) {
                                    db = db.Table;
                                    db = db.reverse();
                                    db.push(category[0]);
                                    category = db.reverse();
                                }
                                var dblist = {
                                    CarouselList: {
                                        source_id: getitem("W_18"),
                                        Request: {},
                                        db: []
                                    },
                                    NoticeBoard: {
                                        source_id: getitem("W_19"),
                                        Request: {},
                                        db: []
                                    },
                                    ImageList: {
                                        source_id: getitem("W_20"),
                                        Request: {},
                                        db: []
                                    }
                                }
                                new ArFor(dblist, function () { 
                                    
                                    cache.render({
                                        id: package.id,
                                        file: './jview/index'
                                    }, {
                                        title: website.name,
                                        icon: website.icon,
                                        style: website.style,
                                        manifest_id: manifest_id,
                                        sys_id: package.id,
                                        login: login,
                                        category: category,
                                        UserAgent: req.UserAgent,
                                        CarouselList: dblist.CarouselList.db,
                                        NoticeBoard: dblist.NoticeBoard.db,
                                        ImageList: dblist.ImageList.db,
                                        PageType: 'home',
                                        CategoryList: category
                                    }, req, res, File);
                                }).forEach(function (item, next) {
                                    if (item && item.source_id) {
                                        new source(item.source_id, item.Request, function (_db) {
                                            if (_db && _db.Table) {
                                                item.db = _db.Table;
                                            } else {

                                            }
                                            next()
                                        })
                                    } else {
                                        next()
                                    }
                                })
                            })
                        }
                    },res)
                })
            })
        }
    };
    return router;
};